package com.qingxin.repertory.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.FarmPark;
import com.qingxin.repertory.entity.Repertory;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
import com.qingxin.ui.bean.TreeNode;

@Service
public class RepertoryService extends BaseService<Repertory, String>{
	
	/**
	 * 获取菜单树数据
	 * @param loginUser
	 * @param isParent
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<TreeNode> getUITreeData(LoginUser loginUser){
		List<TreeNode> list = new ArrayList<TreeNode>();
		StringBuilder hql = new StringBuilder();
		hql.append("select new com.qingxin.park.entity.FarmPark(parkid,parkName)");
		hql.append(" from FarmPark");
		hql.append(" order by createTime asc");
		List<FarmPark> farmParks = listByHql(hql.toString());
		if(farmParks != null){
			for(FarmPark farmPark : farmParks){
				list.add(new TreeNode(farmPark.getParkid(),farmPark.getParkName(),"PARK",null,false));
			}
		}
		return list;
	}
	
	/**
	 * 库存数据
	 * @param pageParam
	 * @param search
	 * @param parkid
	 * @param request
	 * @return
	 */
	public Pager list(PageParam pageParam,String pType,String mType,String search,String parkid, HttpServletRequest request){
		Querier querier = Querier.create("from Repertory r where r.parkid=:parkid");
		querier.setParam("parkid", parkid);
		if(StringUtils.isNotEmpty(pType) && StringUtils.isNotEmpty(mType)){
			querier.append(" and r.productType=:productType");
			querier.setParam("productType", pType);
			
			querier.append(" and r.materialType=:materialType");
			querier.setParam("materialType", mType);
		}else {
			return new Pager();
		}
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (r.materialName like :materialName)");
			querier.setParam("materialName", search);
		}
		querier.append(pageParam.getOrderby(" order by r.createTime desc","r"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	@SuppressWarnings("unchecked")
	public List<Repertory> listForExport(String parkid, String pType, String mType, String search){
		Querier querier = Querier.create("from Repertory r where r.parkid=:parkid");
		querier.setParam("parkid", parkid);
		
		querier.append(" and r.productType=:productType");
		querier.setParam("productType", pType);
		
		querier.append(" and r.materialType=:materialType");
		querier.setParam("materialType", mType);
		
		if(StringUtils.isNotEmpty(search)){
			search = "%"+search.replace("'", "")+"%";
			querier.append(" and (r.materialName like :materialName)");
			querier.setParam("materialName", search);
		}
		
		querier.append(" order by r.createTime desc");
		
		return listByQuerier(querier);
	}
	
	
	
	/**
	 * 作业选择物资使用
	 * @param parkid
	 * @param blockType
	 * @param linkType
	 * @param pageParam
	 * @param request
	 * @return
	 */
	public Pager listSelectData(String parkid, String blockType, String linkType, PageParam pageParam, HttpServletRequest request){
		Querier querier = Querier.create("select new com.qingxin.repertory.entity.Repertory(" +
				"r.repertoryid, r.materialName, r.batchNum, r.producer, r.repertory_num, r.createTime, r.unit)" +
				" from Repertory r where r.parkid=:parkid and r.productType=:productType and r.materialType=:materialType");
		querier.setParam("parkid", parkid);
		querier.setParam("productType", blockType);
		querier.setParam("materialType", linkType);
		querier.append(" order by r.createTime desc");
		
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	
	
	@SuppressWarnings("unchecked")
	public List<Repertory> listRepertory(String parkid, String blockType, String linkType){
		List<Repertory> list = listByHql("from Repertory r where r.parkid=?" +
				" and r.productType=? and r.materialType=? order by r.createTime desc",
				parkid, blockType, linkType);
		return list;
	}
	
	
	
	/**
	 * 保存库存数据
	 * @param repertory
	 * @param parkid
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(Repertory repertory, String parkid, LoginUser loginUser, HttpServletRequest request){
		Date now = new Date();
		repertory.setCreateTime(now);
		repertory.setCreateBy(loginUser.getLogin_name());
		repertory.setAmount(new BigDecimal(0));
		repertory.setUsed_num(new BigDecimal(0));
		repertory.setRepertory_num(new BigDecimal(0));
		
		if(StringUtils.isNotEmpty(repertory.getRepImg())){
			UploadFile.moveTmpTo(repertory.getRepImg(), UploadFile.DIR_REP_IMG);
		}
		
		//repertory.setParkid(parkid);
		this.save(repertory);
		return null;
	}
	/**
	 * 删除库存信息
	 * @param ids
	 * @param request
	 * @return
	 */
	public SysError deleteAll(String[] ids,HttpServletRequest request){
		for(String id : ids){
			Repertory repertory = this.get(id);
			if(repertory == null){
				continue;
			}
			this.delete(repertory);
		}
		return null;
	}
	
	/**
	 * 删除库存信息
	 * @param ids
	 * @param request
	 * @return
	 */
	public SysError delete(String id,HttpServletRequest request){
		Object obj = getByHql("select repertoryid from RepertoryAccount where repertoryid=? and ( type=1 or type=2 ) ", id);
		if(obj==null){
			Repertory repertory = this.get(id);
			this.delete(repertory);
			return null;
		}
		return new SysError("已存在流水账无法删除！");
	}
	
	/**
	 * 更新库存数据
	 * @param repertory
	 * @param uploadFileParam
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(Repertory repertory,LoginUser loginUser, HttpServletRequest request){
		Repertory old = getSafeEntity(repertory.getRepertoryid());
		String fields = "materialName,materialType,ingredient,batchNum,producer,phi,repImg,unit,remark,specification,model";
		
		if(StringUtils.isNotEmpty(repertory.getRepImg()) && !repertory.getRepImg().equals(old.getRepImg())){
			UploadFile.moveTmpTo(repertory.getRepImg(), UploadFile.DIR_REP_IMG);
		}
		
		BeanUtil.setTargetFields(old, repertory, fields, true);
		Updater<Repertory> updater = new Updater<>(old, fields);
		this.update(updater);		
		return null;
	}
	
	
	/**
	 * 判断指定项目名称是否被使用
	 * @param itemName
	 * @param itemId
	 * @param taskId
	 * @return
	 */
	public boolean isNameDuplicate(String materialName,String productType,String materialType, String repertoryid, String parkid){
		if(StringUtils.isEmpty(repertoryid)){
			Object obj = getByHql("select repertoryid from Repertory where materialName=? and productType=? and materialType=? and parkid = ?", materialName,productType,materialType,parkid);
			return obj!=null;
		}else {
			Object obj = getByHql("select repertoryid from Repertory where parkid=? and productType=? and materialType=? and materialName=? and repertoryid<>?",parkid ,productType,materialType,materialName, repertoryid);
			return obj!=null;
		}
	}
}
